<template>
  <d2-container>
    <template slot="header">
      <el-input slot="header" v-model="query.name" :clearable="true" placeholder="姓名" style="margin-bottom: 5px;width:200px;margin-right:10px"></el-input>
      <el-select slot="header" placeholder="角色" v-model="query.roleId" style="margin-bottom: 5px;margin-right:10px">
        <el-option v-for="option in option.role" :key='option.id' :value="option.id" :label="option.name" />
      </el-select>
      <el-button slot="header" icon="el-icon-search" @click="handleSearch">查询</el-button>
      <el-button slot="header" icon="el-icon-refresh"  @click="handleRefresh">重置</el-button>
      <el-button slot="header" icon="el-icon-plus" type="button" @click="() => handleAdd()" v-hasBtn="'user_add'">新增</el-button>
    </template>
      <el-table :data="list.data" border v-loading="list.loading" class="table" ref="multipleTable">
        <el-table-column prop="userName" label="姓名" v-if="hasCol('user_name')">
        </el-table-column>
        <el-table-column prop="loginName" label="登录名" v-if="hasCol('user_loginName')">
        </el-table-column>
        <el-table-column prop="roleName" label="角色" v-if="hasCol('user_role')">
        </el-table-column>
        <el-table-column prop="mark" label="状态" v-if="hasCol('user_status')">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.mark==1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center" v-if="hasCol('user_edit')">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasBtn="'user_edit'">编辑</el-button>
            <el-button type="text" icon="el-icon-refresh" @click="handleResetPwd(scope.row)" v-hasBtn="'user_resetPwd'">重置密码</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)" v-hasBtn="'user_del'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template slot="footer">
        <el-pagination background @size-change="handlePageSize" @current-change="handlePageIndex" :current-page="query.pageIndex" :page-sizes="[10, 20, 30, 50, 150, 200]" layout="total, sizes, prev, pager, next, jumper" :page-size="query.pageSize" :total="list.total">
        </el-pagination>
      </template>
      <Drawer :title="form.type" v-model="form.show" width="720" :mask-closable="false" :styles="styles">
        <el-form ref="form" :model="form.model" :rules="form.rules" label-width="100px">
          <el-form-item label="姓名" prop="userName">
            <el-input v-model="form.model.userName"></el-input>
          </el-form-item>
          <el-form-item label="登录名"  prop="loginName">
            <el-input v-model="form.model.loginName"></el-input>
          </el-form-item>
          <el-form-item label="登录密码"  prop="loginPwd" v-if="form.type == '新增'">
            <el-input type="password" v-model="form.model.loginPwd"></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="roleId">
            <el-select placeholder="角色" v-model="form.model.roleId" class="handle-input" style="width:100%">
              <el-option v-for="option in option.role" :key='option.id' :value="option.id" :label="option.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.model.mark">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div class="iview-drawer-footer">
          <Button style="margin-right: 8px" @click="form.show = false">取 消</Button>
          <Button type="primary" @click="handleSaveEdit">确 定</Button>
        </div>
      </Drawer>  
  </d2-container>
</template>
<script>
    import { roleOption } from '@/api/option'
    import { isSelected } from '@/utils/validator'
    import { getPage,save,remove,resetPwd } from '@/api/system/user'
    import { haveCol } from '@/utils/colPermissions'; // 列权限 
    import md5 from 'js-md5'
    export default {
        data() {
            return {
                query: {
                    name: '',
                    roleId: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                styles: {
                    height: 'calc(100% - 55px)',
                    overflow: 'auto',
                    paddingBottom: '53px',
                    position: 'static'
                },
                list: {
                    loading: false,
                    total: 0,
                    data: []
                },
                option:{
                  role:[]
                },
                form:{
                  loading:false,
                  show:false,
                  type:"新增",
                  model:{
                    id:"",
                    loginName:"",
                    loginPwd:"",
                    userName:"",
                    roleId:"",
                    roleName:"",
                    headImg:"",
                    mark:1,
                    remark:""
                  },
                  rules: {
                      userName: [
                          { required: true, message: '请输入姓名', trigger: 'blur' }
                      ],
                      loginName: [
                          { required: true, message: '请输入登录名', trigger: 'blur' }
                      ],
                      loginPwd: [
                          { required: true, message: '请输入登录密码', trigger: 'blur' }
                      ],
                      roleId: [
                          { validator: isSelected, trigger: 'blur' }
                      ]
                  }
                }
            }
        },
        created() {
            this.getListData();
            this.getRoleOption();
            
        },
        computed: {
            data() {
            }
        },
        methods: {
            getListData() {
              this.list.loading = true;
              getPage(this.query).then(res => {
                this.list.data = res.data.items;
                this.list.total = res.data.totalCount;
                this.list.loading = false;
              })
            },
            getRoleOption(){
              roleOption().then(res => {
                this.option.role = res.data
              })
            },
            handlePageSize(pageSize) {
                this.query.pageSize = pageSize
                this.getListData()
            },
            handlePageIndex(pageIndex) {
                this.query.pageIndex = pageIndex
                this.getListData()
            },
            handleSearch() {
                this.query.pageIndex = 1
                this.query.pageSize = 10
                this.getListData()
            },
            handleRefresh() {
              this.query = {
                name: '',
                roleId: 0,
                pageIndex: 1,
                pageSize: 10
              }
              this.getListData()
            },
            handleAdd(){
              this.form.model = {
                    loginName:"",
                    loginPwd:"",
                    userName:"",
                    roleId:"",
                    roleName:"",
                    headImg:"",
                    mark:1,
                    remark:""
              }
              this.form.type = "新增";
              this.form.show = true;
            },
            handleEdit(row){
              this.form.model = Object.assign({}, row);
              this.form.type = "编辑";
              this.form.show = true;
            },
            handleSaveEdit(){
              this.$refs["form"].validate((valid) => {
                if (valid) {
                  this.form.loading = true
                  if(this.form.type === '新增') {
                    this.form.model.loginPwd = md5(this.form.model.loginPwd)
                  }
                  this.option.role.map(item =>{
                    if(item.id === this.form.model.roleId){
                      this.form.model.roleName = item.name
                    }
                  })
                  save(this.form.model).then(res => {
                    if (res.status === 200) {
                      this.form.show = false;
                      this.$message({
                        message: "操作成功",
                        type: "success"
                      });
                      this.getListData();
                    }
                    this.form.loading = false
                  })
                  }
                  else{
                    this.$message({ message: "输入错误", type: 'error' })
                    return false;
                  }
                })
            },
            handleResetPwd(row){
              this.$prompt('请输入密码', '重置密码', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputType:'password',
              }).then(({ value }) => {
                resetPwd({ 
                    id: row.id,
                    pwd: md5(value)
                  }).then(res => {
                  if (res.status === 200) {
                    this.form.show = false;
                    this.$message({
                      message: "重置成功",
                      type: "success"
                    });
                  }
              }).catch(() => {    
              });
            })
          },
          handleDelete(data) {
            this.$confirm("此操作将永久删除, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
                remove({ id: data.id }).then(res => {
                  if (res.status === 200) {
                    this.form.show = false;
                    this.$message({
                      message: "删除成功",
                      type: "success"
                    });
                    this.getListData();
                  }
                });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除"
                });
              });
          },
          hasCol(e){
            return haveCol(e)
          }
        }
    }

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.iview-drawer-footer{
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
</style>
